/////////////////////////////////////////////////////////////////////////////
// Name:        imageblock.h
// Purpose:     A block of image data
// Author:      Julian Smart
// Modified by:
// Created:     2006-05-11
// RCS-ID:      $$
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_IMAGEBLOCK_H_
#define _WX_IMAGEBLOCK_H_

#include "wx/defs.h"
#include "wx/variant.h"
#include "wx/image.h"

/*!
 * wxImageBlock stores information about an image, in binary in-memory form
 */

class WXDLLIMPEXP_BASE wxDataInputStream;
class WXDLLIMPEXP_BASE wxDataOutputStream;

class wxImageBlock: public wxObject
{
public:
    wxImageBlock();
    wxImageBlock(const wxImageBlock& block);
    ~wxImageBlock();

    void Init();
    void Clear();

    // Load the original image into a memory block.
    // If the image is not a JPEG, we must convert it into a JPEG
    // to conserve space.
    // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
    // load the image a 2nd time.
    virtual bool MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG = true);

    // Make an image block from the wxImage in the given
    // format.
    virtual bool MakeImageBlock(const wxString& filename, wxImage& image, int imageType = wxBITMAP_TYPE_PNG, int quality = 80);
    virtual bool MakeImageBlock(wxImage& image, int imageType = wxBITMAP_TYPE_PNG, int quality = 80);
    virtual bool MakeImageBlock(const wxImage& image, int imageType = wxBITMAP_TYPE_PNG, int quality = 80);

    // Write to a file
    bool Write(const wxString& filename);

    // Write to a stream
    bool Write(wxOutputStream& stream);
    bool Write(wxOutputStream& stream, wxDataOutputStream& dataStream);

    // Write data in hex to a stream
    bool WriteHex(wxOutputStream& stream);

    // Read data in hex from a stream
    bool ReadHex(wxInputStream& stream, int length, int imageType);

    // Read from a stream
    bool Read(wxInputStream& stream);
    bool Read(wxInputStream& stream, wxDataInputStream& dataStream);

    // Copy from 'block'
    void Copy(const wxImageBlock& block);

    // Load a wxImage from the block
    bool Load(wxImage& image);

//// Operators
    void operator=(const wxImageBlock& block);

//// Accessors

    unsigned char* GetData() const { return m_data; }
    size_t GetDataSize() const { return m_dataSize; }
    int GetImageType() const { return m_imageType; }

    void SetData(unsigned char* image) { m_data = image; }
    void SetDataSize(size_t size) { m_dataSize = size; }
    void SetImageType(int imageType) { m_imageType = imageType; }

    bool Ok() const { return GetData() != NULL; }
    
/// Implementation

    /// Allocate and read from stream as a block of memory
    static unsigned char* ReadBlock(wxInputStream& stream, size_t size);
    static unsigned char* ReadBlock(const wxString& filename, size_t size);

    // Write memory block to stream
    static bool WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size);

    // Write memory block to file
    static bool WriteBlock(const wxString& filename, unsigned char* block, size_t size);

    // Returns the image type, or -1, determined from the extension.
    static int DetermineImageType(const wxString& filename);

    // Return an extension for the given file type
    static wxString DetermineExtension(int imageType = -1);

protected:
    // Size in bytes of the image stored.
    // This is in the raw, original form such as a JPEG file.
    unsigned char*      m_data;
    size_t              m_dataSize;
    int                 m_imageType; // wxWin type id
};

#endif
    // _WX_IMAGEBLOCK_H_
